$(document).ready(function($){
	
	$('select.size').change(function(){
		var whatSize = $('.size option:selected').val();
		var custExist = $('#custSize');
		if(whatSize == 'other'){
			$(this).after('<p class="indent highlightBG floatLeft" id="custSize" style="margin-left: 43px;" />');
				$('p.indent').append('<h4>Indicate a size in inches</h4><label>Width</label><input type="text" id="width" size="3" value="0" /><label>Length</label><input type="text" id="length" size="3" value="0" /><label>Thickness</label><input type="text" id="thickness" size="3" value="0" /></p><p><input type="button" id="updateCustom" value="Update Prices" /></p>');
				$('.sel_price').text('0.00');
		}else if(custExist.length == '1'){
			$(custExist).remove();
		}
	})		
	
	loadPrices();
		
	//Add To Cart Function
	$('.purchase').live('click', function(){
		var product = $(this).parent().parent();
		var product_ID = $(product).attr('id');
		var title = $(product).find('.title').text();
		var qty = $(product).find('.quantity').val();
		//Size and Price					
		if($('.size option:selected').val() != 'other'){
			var size = $(product).find('select.size option:selected').text();
			var price = $(product).find('input[type=radio]:checked').next('label');						
		}else{
			var cWidth = $('#width').val(); 
			var cLength = $('#length').val();
			var cThick = $('#thickness').val();
			var sqInch = cWidth*cLength;
			var size = cWidth+"x"+cLength+"x"+cThick;
			
			if(product_ID == 'merv8'){
				var level1 = '29.95';
				var level2 = '34.95';
				if(sqInch > 36 && sqInch < 750){
					var price = level1;
				}else{
					var price = level2
				}  				
			}else if(product_ID == 'merv11'){
				var level1 = '34.95';
				var level2 = '39.95';
				if(sqInch > 36 && sqInch < 750){
					var price = level1;
				}else{
					var price = level2;
				}  				
			}else if(product_ID == 'carb_small' || product_ID == 'carb_medium' || product_ID == 'carb_large'){
				var level1 = '54.95';
				var level2 = '64.95';
			}
		}
		
		var sizeVal = $(product).find('select.size option:selected').val();		
		var freq = $(product).find('select.frequency option:selected').text();
		var freqVal = $(product).find('select.frequency option:selected').val();					
		var selPrice = $(price).find('span.sel_price').text();
		var selFreq = $(price).find('span.sel_freq').text();
		var planType = $(price).attr('class');				
		var cartID = product_ID+size+planType+freqVal;
		var pay = $(product).find('.payment-plan input[type="radio"]:checked').next('label');
		var selPayment = $(pay).find('.freq_num').text();
		var trialPrice = $(product).find('.planA .sel_price').text();		
		
		checkValid();
		
		function checkValid(){
			if (sizeVal == '0'){			
				alert("Please select a valid size");
			}else{
				addToCart();
			}
		}
		
	$('.remove_item').live('click', function(){
		var cartLength = $('.cart_item').length;
		
		if(cartLength == 1){
			$('.checkOut').remove();
		}
		
		$(this).parent().parent().remove();
			$('#'+cartID).remove();
				$('.termsconditions').remove();
	})									
		
	function addToCart(){
		//if($('#'+cartID).length>0){	//Exists, update quantity and total
		//	var cart_Quantity = parseFloat($('#'+cartID).find('.cart_quantity').text());
		//	var cart_TotalQuanty = parseInt(qty) + parseInt(cart_Quantity);
	   //	var cartPrice = parseFloat($('#'+cartID).find('.cart_price').text());
							
	//		$('#'+cartID).find('.cart_quantity').text(cart_TotalQuanty);
	//		$('#'+cartID).find('.cart_price').text((Math.round((parseFloat(selPrice)+cartPrice)*100)/100).toFixed(2));						
	//Create New Product in Cart
					
			$('.cart-content').html(
				"<div class='cart_item' id='"+cartID+"'><h4 class='cart-title'>"+title+"</h4><p>Size: <span class='cart-size'>"+size+"</span></p><p>Quantity: <span size='2' class='cart_quantity'>"+qty+"</span></p><p>Payments: $<span class='cart_price'>"+selPrice+"</span></p><p>Delivery: Every <span class='cart_freq'>"+freqVal+"</span> month(s)</p><p><input type='button' value='Remove' class='remove_item' /></div>"
			)
		}
		createPayPal();			
		
	function createPayPal(){
		var obj = $('#'+cartID);
		var title = obj.find('.cart-title').text();
		var size = obj.find('.cart-size').text();
		var price = obj.find('.cart_price').text();
		
		$('form').append(
			"<div class='item' id='"+cartID+"'><input type='hidden' name='item_name' value='"+title+"' /><input type='hidden' name='on0' value='Size' /><input type='hidden' name='os0' value='"+size+"' /><input type='hidden' name='on1' value='Quantity' /><input type='hidden' name='os1' value='"+qty+"' /><input type='hidden' name='on2' value='Shipping Frequency' /><input type='hidden' name='os2' value='"+freqVal+" month(s)' /><input type='hidden' name='a3' value='"+price+"' /><input type='hidden' name='p3' value='"+selPayment+"' /><input type='hidden' name='t3' value='M' /><input type='hidden' name='a1' value='"+trialPrice+"' /></div>"
		)	
	}
		
		if($('.cart .checkOut').length== '0'){
			$('.cart-content').after('<p class="termsconditions"><input type="checkbox" id="terms_conditions" />I agree to SendMeAFilter.com&lsquo;s <a href="#" id="terms" title="Click to view our terms and conditions">Terms and Conditions</a></p><div id="checkout_button"></div><input type="submit" class="checkOut" value="Checkout" disabled="disabled" />');
		}
		
		$('#terms_conditions').live('click', function(){
			if($(this).attr("checked")=="checked"){
				$('.checkOut').removeAttr("disabled").css('z-index','100');
			}else{
				$('.checkOut').attr("disabled","disabled").css('z-index','0');			
			}
		})
	}							
	)
	
	//Update Prices In Payment Plan Options When Quantity or Frequency are Changed 
	$('.frequency, .quantity').change(function(){
		loadPrices();
	})
	
});

$("#checkout_button").live('click', function(){
	if($(".checkOut").attr("disabled")=="disabled"){
		alert("You must agree to our Terms and Conditions before continuing.")
	}else{
		//alert("Enabled");
	}
})

function loadPrices(){
	var obj = $('.product')
	
	$(obj).each(function(){
		var price = $(this).find('.price').text();
		var priceClean = parseFloat(removeDollar(price));
		var priceCleanA = priceClean.toFixed(2);					
		var qty = $(this).find('.quantity').val();
		var freq = $(this).find('select.frequency option:selected').val();
		var priceCleanB = (Math.round((priceCleanA*qty/parseInt(freq))*100)/100).toFixed(2);
		
		//Now the labels
		$(this).find('label.planA').html("$<span class='sel_price'>"+priceCleanA*qty+"</span><span class='sel_freq'> every <span class='freq_num'>"+freq+"</span> month(s)</span>");
		$(this).find('label.planB').html("$<span class='sel_price'>"+priceCleanB+"</span><span class='freq_num' style='display: none'>1</span><span class='sel_freq'> per month</span>");					
	})
}			

//Remove Dollar Sign From Prices	
function removeDollar(str){
	re = /^\$|,/g;
	return str.replace(re, "");
}


//var getit = $.get('/pricing',{},function(json,pricing){ 
//pricing.data = JSON.parse(json); 
//alert(json);
//});
//{ 
//	tax:{ UT: 6.85 },
//	multiFilterDiscount: { amount:-5.00, perAdditional:true },
//	discount: [
//		          { code:'family', title:'Family discount (20% off)',  percent:20.0 },
//		          { code:'megaplex', title:'Megaplex customer discount (5% off)', percent:5.0 },
//	],
//	filter: [ 
//	         	// 1 inch
//		        { 
//					code:'M8-1-D', name:'Merv 8 Discounted', thickness: 1, price:19.95,  
//					sizes: '10x10, 10x20, 12x20, 14x20, 15x20, 16x20' 
//		        },
//		        { 
//		        	code:'M8-1-S', name:'Merv 8 Standard', thickness: 1, price:24.95,  
//		        	sizes: '10x10 10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x25 18x20 18x24 18x25 20x20 20x24 20x25 24x24' 
//		        },
//		        { 
//		        	code:'M11-1-S', name:'Merv 11 Standard', thickness: 1, price:29.95,  
//		        	sizes: '10x10 10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x25 18x24 18x25 20x20 20x24 20x25 20x30 24x24 25x25' 
//		        },
//		        { 
//		        	code:'C-1-1-S', name:'Carbon Standard', thickness: 1, price:39.95,  
//		        	sizes: '10x10 10x20 12x12 12x20 12x24 14x24 14x20 14x25 15x20 16x20 16x25 18x24 18x25 20x20 20x24 20x25' 
//		        },
//		        { 
//		        	code:'C-1-2-S', name:'Carbon Standard', thickness: 1, price:44.95,  
//		        	sizes: '10x24 14x14 16x16 16x24 18x18 18x20 20x22 22x22 24x24 25x25' 
//		        },
//		        { 
//		        	code:'C-1-3-S', name:'Carbon Standard', thickness: 1, price:49.95,  
//		        	sizes: '10x30 12x30 14x30 15x30 16x30 18x30 20x30 24x30' 
//		        },
//		        { 
//		        	code:'M8-1-C', name:'Merv 8 Custom', thickness: 1,  
//		        	calc: [
//		        	      { price:29.95, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:34.95, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'M11-1-C', name:'Merv 11 Custom', thickness: 1,  
//		        	calc: [
//		        	      { price:34.95, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:39.95, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'C-1-C', name:'Carbon Custom', thickness: 1,  
//		        	calc: [
//			        	      { price:54.95, sideUnder:30, fromSqInches:150, toSqInches:399 },
//			        	      { price:64.95, sideUnder:30, fromSqInches:400, toSqInches:900 },
//			        	      { price:69.95, sideUnder:60, fromSqInches:150, toSqInches:399 },
//			        	      { price:74.95, sideUnder:60, fromSqInches:400, toSqInches:900 },
//		        	] 
//		        },
//		        // 2 inch
//		        { 
//		        	code:'M8-2-S', name:'Merv 8 Standard', thickness: 2, price:27.45,  
//		        	sizes: '10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x24 16x25 18x20 18x24 18x25 20x20 20x24 20x25' 
//		        },
//		        { 
//		        	code:'M11-2-S', name:'Merv 11 Standard', thickness: 2, price:32.95,  
//		        	sizes: '10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x24 16x25 18x24 18x25 20x20 20x24 20x25 24x24' 
//		        },
//		        { 
//		        	code:'C-2-1-S', name:'Carbon Standard', thickness: 2, price:43.95,  
//		        	sizes: '10x20 12x12 12x20 12x24 14x20 14x25 15x20 16x20 16x25 18x20 18x24 18x25 20x20 20x24 20x25' 
//		        },
//		        { 
//		        	code:'C-2-2-S', name:'Carbon Standard', thickness: 2, price:49.45,  
//		        	sizes: '16x16 16x24 18x18 18x20 24x24 25x25' 
//		        },
//		        { 
//		        	code:'C-2-3-S', name:'Carbon Standard', thickness: 2, price:54.95,  
//		        	sizes: '20x30' 
//		        },
//		        { 
//		        	code:'M8-2-C', name:'Merv 8 Custom', thickness: 2,  
//		        	calc: [
//		        	      { price:32.95, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:39.84, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'M11-2-C', name:'Merv 11 Custom', thickness: 2,  
//		        	calc: [
//		        	      { price:38.45, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:47.94, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'C-2-C', name:'Carbon Custom', thickness: 2,  
//		        	calc: [
//		        	      { price:60.45, sideUnder:30, fromSqInches:150, toSqInches:399 },
//		        	      { price:71.45, sideUnder:30, fromSqInches:400, toSqInches:900 },
//		        	      { price:76.95, sideUnder:60, fromSqInches:150, toSqInches:399 },
//		        	      { price:82.45, sideUnder:60, fromSqInches:400, toSqInches:900 },
//		        	] 
//		        },
//		        // 4 inch
//		        { 
//		        	code:'M8-4-S', name:'Merv 8 Standard', thickness: 4, price:29.94,  
//		        	sizes: '12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24' 
//		        },
//		        { 
//		        	code:'M11-4-S', name:'Merv 11 Standard', thickness: 4, price:35.94,  
//		        	sizes: '12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24' 
//		        },
//		        { 
//		        	code:'C-4-1-S', name:'Carbon Standard', thickness: 4, price:47.94,  
//		        	sizes: '12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24' 
//		        },
////		        { 
////		        	code:'C-4-2-S', name:'Carbon Standard', thickness: 4, price:53.94,  
////		        	sizes: '' 
////		        },
////		        { 
////		        	code:'C-4-3-S', name:'Carbon Standard', thickness: 4, price:59.94,  
////		        	sizes: '' 
////		        },
//		        { 
//		        	code:'M8-4-C', name:'Merv 8 Custom', thickness: 4,  
//		        	calc: [
//		        	      { price:35.94, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:48.93, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'M11-4-C', name:'Merv 11 Custom', thickness: 4,  
//		        	calc: [
//		        	      { price:41.94, sideUnder:30, fromSqInches:36, toSqInches:750 },
//		        	      { price:57.93, sideUnder:60, fromSqInches:180, toSqInches:1500 },
//		        	] 
//		        },
//		        { 
//		        	code:'C-4-C', name:'Carbon Custom', thickness: 4,  
//		        	calc: [
//		        	      { price:65.94, sideUnder:30, fromSqInches:150, toSqInches:399 },
//		        	      { price:77.94, sideUnder:30, fromSqInches:400, toSqInches:900 },
//		        	      { price:83.94, sideUnder:60, fromSqInches:150, toSqInches:399 },
//		        	      { price:104.93, sideUnder:60, fromSqInches:400, toSqInches:900 },
//		        	] 
//		        },
//		        // 5 inch
//		        { 
//		        	code:'M8-5-S', name:'Merv 8 Standard', thickness: 5, price:39.95,  
//		        	sizes: '20x20 20x25' 
//		        },
//		        { 
//		        	code:'M11-5-S', name:'Merv 11 Standard', thickness: 5, price:49.95,  
//		        	sizes: '20x20 20x25' 
//		        },
//		        { 
//		        	code:'C-5-S', name:'Carbon Standard', thickness: 5, price:59.95,  
//		        	sizes: '20x20 20x25' 
//		        },
//	] 
//};		



